package com.audaque.springboot.foshanupload.reactor.webfluxdemo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.audaque.springboot.foshanupload.reactor.webfluxdemo.model.qo.QueryRequest;
import com.audaque.springboot.foshanupload.reactor.webfluxdemo.service.iface.MusicDetailService;
import com.audaque.springboot.foshanupload.reactor.webfluxdemo.model.pojo.MusicDetail;
import com.audaque.springboot.foshanupload.reactor.webfluxdemo.repository.MusicDetailRepository;
import com.audaque.springboot.foshanupload.reactor.webfluxdemo.util.ReactiveMongoHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import java.util.Date;
import java.util.Map;

/**
 * @author zgb
 * @desc ...
 * @date 2023-07-23 02:20:39
 */
@Service
public class MusicDetailServiceImpl implements MusicDetailService {
    @Autowired
    private ReactiveMongoHelper reactiveMongoHelper;
    @Autowired
    private MusicDetailRepository musicDetailRepository;

    //添加音乐信息
    public void addMusic(MusicDetail musicDetail) {
        // 先查询
        Mono<MusicDetail> music = musicDetailRepository.
                findByMusicNameAndAuthor(musicDetail.getMusicName(), musicDetail.getAuthor());
        if (music != null) {
            return;
        }
        musicDetail.setAddTime(new Date());
        musicDetailRepository.save(musicDetail);
        return;
    }

    //修改音乐信息
    public void editMusic(String id, MusicDetail musicDetail) {
        //先查询
        Mono<MusicDetail> optional = musicDetailRepository.findById(id);
        MusicDetail music = optional.block();
        if (music == null) {
            return;
        }
        //执行修改保存
        music.setMusicName(musicDetail.getMusicName());
        music.setMusicType(musicDetail.getMusicType());
        music.setAuthor(musicDetail.getAuthor());
        music.setAlbum(musicDetail.getAlbum());
        musicDetailRepository.save(music);
        return;
    }

    //执行删除
    public void delMusic(String id) {
        // 先查询
        Mono<MusicDetail> optional = musicDetailRepository.findById(id);
        MusicDetail music = optional.block();
        if (music == null) {
            return;
        }
        //删除
        musicDetailRepository.deleteById(id);
        return;
    }

    //按条件查询，并且分页
    public Mono<Page<MusicDetail>> getMusic(int page, int size, QueryRequest queryRequest) {
        // 判断查询条件非空
        if (queryRequest == null) {
            queryRequest = new QueryRequest();
        }
        //创建条件值对象
        MusicDetail music = new MusicDetail();
        //按照作者、音乐名称、专辑名模糊查询

        //在条件匹配器中我没有设置musicType这个参数
        //没有设置的话它默认是按精确匹配
        //判断各参数是否为空
        if (StringUtils.isNotEmpty(queryRequest.getMusicType())) {
            music.setMusicType(queryRequest.getMusicType());
        }
        if (StringUtils.isNotEmpty(queryRequest.getMusicName())) {
            music.setMusicName(queryRequest.getMusicName());
        }
        if (StringUtils.isNotEmpty(queryRequest.getAlbum())) {
            music.setAlbum(queryRequest.getAlbum());
        }
        if (StringUtils.isNotEmpty(queryRequest.getAuthor())) {
            music.setAuthor(queryRequest.getAuthor());
        }

        if (page <= 0) {
            page = 1;
        }
        //page=0代表第1页，所以这里要从0开始
        page = page - 1;
        if (size <= 5) {
            size = 5;
        }
        //获取分页对象
        Map<String, Object> map = BeanUtil.beanToMap(music);
        Mono<Page<MusicDetail>> allWithCriteriaByPagination = reactiveMongoHelper.getAllWithCriteriaByPagination(page, size, map, MusicDetail.class);
        return allWithCriteriaByPagination;

    }

}
